<template>
  <!-- 轮播列表 -->
  <div class="info">
    <template>
      <el-skeleton :loading="loading" />
    </template>
    <template v-if="!loading">
      <el-form :model="searchInfo" inline></el-form>
      <el-form inline>
        <el-form-item>
          <el-button type="success" @click="openWin()">添加</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="adList" border>
        <el-table-column label="编号" prop="bannerId"/>
        <el-table-column label="轮播名称" prop="name"/>
        <el-table-column label="轮播连接地址" prop="url"/>
<!--        <el-table-column label="轮播图片地址" prop="src"/>-->
        <el-table-column label="轮播排序值" prop="sortNum">
          <template slot-scope="scope">
              <el-popover trigger="hover" placement="top">
                  <el-input placeholder="请输入排序值" v-model="scope.row.sortNum">
                  <template slot="append">
                    <el-button type="primary" @click="changeSortNum(scope.row.bannerId,scope.row.sortNum)">点击修改</el-button>
                  </template>
                </el-input>
                <div slot="reference" class="name-wrapper">
                  <el-tag size="medium">{{ scope.row.sortNum }}</el-tag>
                </div>
              </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="轮播类型" prop="type">
          <template slot-scope="scope">
            <el-button type="danger" v-if="scope.row.type==1">首页</el-button>
            <el-button type="success" v-if="scope.row.type==2">热门</el-button>
          </template>
        </el-table-column>
        <el-table-column label="信息状态" prop="status">
          <template slot-scope="scope">
            <el-button size="small" type="success" v-if="scope.row.status===1" @click="changStatus(scope.row)">启用</el-button>
            <el-button size="small" v-else type="danger" @click="changStatus(scope.row)" >禁用</el-button>
          </template>
        </el-table-column>
<!--        <el-table-column label="轮播详情" width="110px">-->
<!--          <template slot-scope="scope">-->
<!--            <el-button type="primary" size="small" @click="showInfo(scope.row.bannerId)">轮播详情</el-button>-->
<!--          </template>-->
<!--        </el-table-column>-->
        <el-table-column label="操作" width="150px">
          <template slot-scope="scope">
            <el-button type="success" size="small" @click="openWin(scope.row)">修改</el-button>
            <el-button type="danger" size="small" @click="delInfo(scope.row.bannerId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination layout="total,prev,pager,next,sizes,jumper"
                     :total="pageInfo.totalCount"
                     :page-size="pageInfo.size"
                     :current-page="pageInfo.current"
                     :page-sizes="[5,10,20,30,50,100]"
                     @size-change="sizeChange"
                     @current-change="currentChange"
      />
    </template>
<!--    <el-dialog title="轮播详情" :visible.sync="adInfoVisible">-->
<!--      <AdInfo ref="adInfo"/>-->
<!--    </el-dialog>-->
  </div>
</template>

<script>
// import AdInfo from '@/views/ad/AdInfo'
export default {
  name: 'Ad',
  // components: { AdInfo },
  data () {
    return {
      loading: false,
      searchInfo: {
        page: 1,
        limit: 10
      },
      adList: [],
      pageInfo: {
        totalCount: 0,
        currPage: 0,
        pageSize: 0,
        totalPage: 0
      }
      // adInfoVisible: false
    }
  },
  methods: {
    changeSortNum (bannerId, sortNum) {
      this.$axios.post('/sys/banner/sort', { bannerId, sortNum }).then(({ data }) => {
        if (data.code === 200) {
          this.$alert('修改成功').then(() => {
            this.reload('ad')
          })
        } else {
          this.$alert('修改失败')
        }
      })
    },
    getList () {
      this.$http.get('/sys/banner/list', this.searchInfo).then(({ data }) => {
        this.loading = false
        if (data.code === 200) {
          this.adList = data.data.list
          this.pageInfo.totalCount = data.data.totalCount
          this.pageInfo.currPage = data.data.currPage
          this.pageInfo.pageSize = data.data.pageSize
          this.pageInfo.totalPage = data.data.totalPage
        } else {
          this.$alert('加载失败')
        }
      })
    },
    sizeChange (limit) {
      this.searchInfo.limit = limit
      this.getList()
    },
    currentChange (page) {
      this.searchInfo.page = page
      this.getList()
    },
    openWin (id) {
      if (id) {
        this.$router.push('AdEdit?bannerId=' + id.bannerId)
      } else {
        this.$router.push('AdEdit')
      }
    },
    delInfo (bannerId) {
      this.$confirm('确定删除该数据吗？', '询问').then(() => {
        this.$axios.delete(`/sys/banner/delete/${bannerId}`).then(({ data }) => {
          if (data.code === 200) {
            this.$message({
              message: '删除成功',
              type: 'success',
              onClose: () => {
                this.getList()
              }
            })
          } else {
            this.$alert('删除失败！', { type: 'error' })
          }
        })
      })
    },
    changStatus (ad) {
      this.$confirm(`确定${ad.status === 1 ? '禁用' : '启用'}该轮播吗？`).then(() => {
        this.$http.post('/sys/banner/status', { bannerId: ad.bannerId, status: ad.status === 1 ? 0 : 1 }).then(({ data }) => {
          if (data.code === 200) {
            this.$message.success('更改状态成功')
            this.getList()
          }
        })
      })
    }
    // showInfo (bannerId) {
    //   this.adInfoVisible = true
    //   this.$nextTick(() => {
    //     this.$refs.adInfo.init(bannerId)
    //   })
    // }
  },
  created () {
    this.$nextTick(() => {
      this.getList()
    })
  }
}
</script>

<style scoped>

</style>
